import {Component} from '@angular/core';
import {App, IonicPage, NavController, NavParams} from 'ionic-angular';
import {ActivityDetailPage} from "../activity-detail/activity-detail";
import {UserPage} from "../user/user";
import {HomePage} from "../home/home";
import {ROUTER} from "../../service/router.service";
import {LocalStorage} from "../../utils/localstorage.utils";
import {ACTIVITY} from "../../api/activity.api";
import {PageParam} from "../../api/api.service";
import {EventService} from "../../service/event.service";
import moment from "moment"


@IonicPage()
@Component({
  selector: 'page-activity',
  templateUrl: 'activity.html'
})
export class ActivityPage {
  pageParam:PageParam;
  hasMore:boolean = true;
  titles = [
    {
      img: './assets/images/cyd.jpg',
      title: '这里是头条新闻的地盘'
    }
  ]
  activities = [];

  constructor(public navCtrl:NavController,
              public navParams:NavParams,
              private app:App,
              private ACTIVITY:ACTIVITY,
              public eventService:EventService,
              private ROUTER:ROUTER, private ls:LocalStorage) {
  }

  gotoActivityDetailPage(v) {
    this.ls.set('actId', v.id);
    this.ROUTER.go(ROUTER.ACTIVITYDETAIL);
  }

  ionViewDidEnter() {
    document.title = '活动';
    this.doRefresh()
    this.eventService.subscribe(EventService.REFRESH_USER, ()=> {
      this.doRefresh()
    })
  }

  ionViewDidLoad() {
    // document.title='活动';
    console.log('ionViewDidLoad Activity');
  }

  doRefresh(refresher?) {
    this.pageParam = new PageParam()
    this.ACTIVITY.getList(v => {
      this.activities = v.content
      console.log(this.activities);
      this.hasMore = !v.last;
      if (refresher) {
        refresher.complete();
      }
    }, this.pageParam);
  }

  formatDate(date):string {
    return moment(date).format('YYYY-MM-DD')
  }

  get canLoadMore():boolean {
    return this.hasMore;
  }

  doInfinite(infiniteScroll) {
    ++this.pageParam.page;
    this.ACTIVITY.getList(v => {
      this.hasMore = !v.last;
      this.activities = this.activities.concat(v.content);
      infiniteScroll.complete();
    }, this.pageParam);
  }

}
